package main;

import gui.GUIHandler;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import cardmanager.CardManager;

import login.LoginHandler;

import config.ConfigHandler;

import network.GameServerClient;

import app.AppGlobals;

public class MainApplication {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//INIT STUFF like loading configs

		updateLAF();
		//start gui
		
		initConfig();
		AppGlobals.gameServer = new GameServerClient(AppGlobals.launcherConfig.serverAddress, AppGlobals.launcherConfig.serverPort);
		AppGlobals.gameServer.start();
		if(AppGlobals.launcherConfig.autoLogin){
			AppGlobals.localUserName = AppGlobals.launcherConfig.defaultUsername;
			LoginHandler.sendLogin(AppGlobals.launcherConfig.defaultUsername, AppGlobals.launcherConfig.password);
		}else{
			CardManager.load("cards.cdb");
			GUIHandler.launchMainWindow();
		}
	

		
	}
	
	
	public static void updateLAF(){

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		
		try {
		  // UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
			//UIManager.setLookAndFeel ( "com.alee.laf.WebLookAndFeel" );
		} catch (Exception e) {
		    e.printStackTrace();
		}
		
//	    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
//	        if ("Nimbus".equals(info.getName())) {
//	            try {
//					UIManager.setLookAndFeel(info.getClassName());
//				} catch (ClassNotFoundException | InstantiationException
//						| IllegalAccessException
//						| UnsupportedLookAndFeelException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//	            break;
//	        }
//	    }

	}
	
	public static void initConfig(){
		String xmlConfig = ConfigHandler.getXMLStringFromFile();
		if(xmlConfig.equals("")){
			ConfigHandler.generateNewConfigFile();
		}else{
			ConfigHandler.loadConfigFile(xmlConfig);
		}
		if(AppGlobals.launcherConfig != null){
			ConfigHandler.createXMLConfigFile();
		}
	}

}
